﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.ServiceModel;
using Contracts;


namespace Presentation
{
    public partial class Update : System.Web.UI.Page
    {
        private IProjectBService channelObject;

        public Update()
        {
            ChannelFactory<IProjectBService> ChannelFactory = new ChannelFactory<IProjectBService>("ProjectBService");
            channelObject=ChannelFactory.CreateChannel();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Department> depList = new List<Department>();
                depList=channelObject.GetAllDepartment();
                
                DropDownListDepartment.Items.Clear();
                DropDownListDepartment.Items.Capacity = depList.Count;
                DropDownListDepartment.Items.Insert(0, "Please Select");

                foreach (var a in depList)
                {
                    ListItem b = new ListItem(a.DepartmentName);
                    if (!DropDownListDepartment.Items.Contains(b))
                    {
                        DropDownListDepartment.Items.Insert(a.DepartmentId, a.DepartmentName);
                    }

                }

                string id = Request["ID"];
                int TeacherID = Convert.ToInt32(id);
                Teacher tea = new Teacher()
                {
                    TeacherId = TeacherID
                };

                List<Teacher> teacherList = new List<Teacher>();
                 teacherList = channelObject.GetTeacher(tea);




                 foreach (var a in teacherList)
                {
                   TeacherNameBox.Text = a.TeacherName;
                  
                   Department dep = new Department() 
                   {
                       DepartmentId = a.DepartMent.DepartmentId
                   };
                   List<Department> dList = new List<Department>();
                    depList = channelObject.GetDepartment(dep);

                    foreach (var b in depList)
                    {
                        DropDownListDepartment.SelectedValue=b.DepartmentName;//figure how to get departmentname through dropdownlist
                        //DropDownListDepartment.SelectedValue = b.DepartmentId.ToString();
                    }
                   
                   if (a.TeacherGender.Trim().Equals("Male"))
                   {
                       RadioButtonList1.SelectedIndex = 0;
                   }
                   else
                    {
                        RadioButtonList1.SelectedIndex = 1;
                    }
                }
                
                
            }
            
        }

        protected void Button_Update_Click(object sender, EventArgs e) //return to teacher list page
        {
            string id = Request["ID"];
            int TeacherID = Convert.ToInt32(id);
            string str=null;
            
            Department dep = new Department()
            {
                DepartmentId = DropDownListDepartment.SelectedIndex
            };

            List<Department> depList = new List<Department>();
            depList = channelObject.GetDepartment(dep);
            
            
            Department myDep = new Department();
            foreach (var b in depList)
            {
            
                myDep = b;
            }

            if(0==RadioButtonList1.SelectedIndex)
            {
                str="Male";
            }
            else
            {
                str="Female";
            }
            Teacher a =new Teacher
            {
                TeacherId = TeacherID,
                 TeacherName = TeacherNameBox.Text,
                 TeacherGender = str,
                 DepartMent = myDep
            };

            

            if (channelObject.UpdateTeacher(a))
            { 
                Response.Write("<script>alert('Update Succeed');window.location.href='TeacherList.aspx';</script>");
            }
            else
                Response.Write("<script>alert('Update Failed');window.location.href='TeacherList.aspx';</script>");
            Response.End();

        }
    }
}